package com.dxj1992.service.impl;


import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dxj1992.entity.AdminPermissions;
import com.dxj1992.entity.AdminUsers;
import com.dxj1992.entity.Ztree;
import com.dxj1992.mapper.AdminPermissionsMapper;
import com.dxj1992.mapper.AdminRolePermissionsMapper;
import com.dxj1992.service.IAdminPermissionsService;
import com.dxj1992.utils.RedisUtil;
import com.dxj1992.utils.Tree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2022-03-31
 */
@Service
public class AdminPermissionsServiceImpl extends ServiceImpl<AdminPermissionsMapper, AdminPermissions> implements IAdminPermissionsService {

    @Autowired
    private AdminPermissionsMapper adminPermissionsMapper;
    @Autowired
    private AdminRolePermissionsMapper adminRolePermissionsMapper;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public List<Ztree> menuTreeData(Integer roleId) {
        if (roleId != null) {
            List<AdminPermissions> adminPermissions = adminPermissionsMapper.selectList(null);
            Map<String, Object> map = new HashMap<>();
            map.put("admin_role_id", roleId);

            List<Integer> ids = adminRolePermissionsMapper.selectByMap(map).stream()
                    .map(a -> a.getAdminPermissionId())
                    .collect(Collectors.toList());

            return getZtrees(adminPermissions, ids);

        } else {
            List<AdminPermissions> adminPermissions = adminPermissionsMapper.selectList(null);
            return getZtrees(adminPermissions, null);
        }
    }

    @Override
    public List<AdminPermissions> selectMenu(AdminUsers user) {
        List<AdminPermissions> adminPermissions = null;
        String key = "admin:menu:role";
        Boolean hexists = redisUtil.hexists(key, user.getId().toString());
        if (!hexists) {
            if (user.isAdmin()) {
                QueryWrapper<AdminPermissions> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(AdminPermissions::getStatus, 0).in(AdminPermissions::getMenuType,"M", "C").orderByAsc(AdminPermissions::getParentId).orderByAsc(AdminPermissions::getSortOrder);
                adminPermissions = adminPermissionsMapper.selectList(queryWrapper);
            } else {
                adminPermissions = adminPermissionsMapper.selectListByUserId(user);
            }
            redisUtil.hsetnx(key, user.getId().toString(), JSONUtil.toJsonStr(adminPermissions));
        } else {
            String hget = redisUtil.hget(key, user.getId().toString());
            adminPermissions = JSONUtil.toList(hget, AdminPermissions.class);
        }
        return Tree.getMenu(adminPermissions, 0);
    }

    @Override
    public List<AdminPermissions> selectMenuByUser(AdminUsers user) {
        List<AdminPermissions> adminPermissions = null;
        String key = "admin:menu:perms";
        Boolean hexists = redisUtil.hexists(key, user.getId().toString());
        if (!hexists) {
            if (user.isAdmin()) {
                QueryWrapper<AdminPermissions> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(AdminPermissions::getStatus, 0).in(AdminPermissions::getMenuType,"M", "C", "F");
                adminPermissions = adminPermissionsMapper.selectList(queryWrapper);
            } else {
                adminPermissions = adminPermissionsMapper.selectMenuByUser(user);
            }
            redisUtil.hsetnx(key, user.getId().toString(), JSONUtil.toJsonStr(adminPermissions));
        } else {
            String hget = redisUtil.hget(key, user.getId().toString());
            adminPermissions = JSONUtil.toList(hget, AdminPermissions.class);
        }
        return adminPermissions;
    }


    private List<Ztree> getZtrees(List<AdminPermissions> adminPermissions, List<Integer> ids) {
        List<Ztree> ztrees = new ArrayList<>();
        for (AdminPermissions adminPermission : adminPermissions) {
            Ztree ztree = new Ztree();
            ztree.setId(adminPermission.getId());
            ztree.setPId(adminPermission.getParentId());
            ztree.setName(adminPermission.getMenuName());
            ztree.setTitle(adminPermission.getMenuName());
            if (ids != null && ids.contains(adminPermission.getId())) {
                ztree.setChecked(true);
            }
            ztrees.add(ztree);
        }
        return ztrees;
    }
}
